package com.space.school.services.impl;

import com.space.school.dao.ProductInfoDao;
import com.space.school.dao.impl.ProductInfoDaoImpl;
import com.space.school.domain.Category;
import com.space.school.domain.ProductInfo;
import com.space.school.services.ProductInfoService;
import com.space.school.utils.PageBean;

import java.util.List;
import java.util.Map;

public class ProductInfoServiceImpl implements ProductInfoService {
    private ProductInfoDao productInfoDao = new ProductInfoDaoImpl();
    @Override
    public PageBean<ProductInfo> search(int currentPage, int pageSize, Map<String, String[]> map) {
        //        从第几个数据开始
        int start=(currentPage-1)*pageSize;
        List<ProductInfo> list=productInfoDao.search(start,pageSize,map);
        PageBean<ProductInfo> pb=new PageBean<ProductInfo>();
        pb.setList(list);
//        总数据数
        int count=productInfoDao.count(map);
        pb.setTotalCount(count);
//        每页显示数据数
        pb.setRows(pageSize);
//        当前页
        pb.setCurrentPage(currentPage);
//        总页数
//        int Totalpage=(int)Math.ceil((double)count/pageSize);
        int totalPage =(count%pageSize==0 ? count/pageSize : count/pageSize+1);
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public void save(ProductInfo productInfo) {
         productInfoDao.save(productInfo);
    }

    @Override
    public List<Category> findAll() {
        return productInfoDao.findAll();
    }

    @Override
    public ProductInfo findById(int id) {
        return productInfoDao.findById(id);
    }

    @Override
    public void update(Map<String, String[]> map) {
        productInfoDao.update(map);
    }
}
